<?php
 	if ( ! defined('BASEPATH')) exit('No direct script access allowed');

 	class MY_Controller extends CI_Controller{

    public $user_data = array();
    public $logged_in = 0;
    public $offset = 10;
    public $school_id;
    public $share_user = 0;
        function __construct() {
            parent::__construct();

            //判断分享用户
            if(!empty($_SERVER['QUERY_STRING']) && isset($_GET['share'])){
                $this->share_user = $_GET['share'];
                $this->session->set_userdata('share_user', $this->share_user);
            }else{
                $share = $this->session->userdata('share_user');
                if(!empty($share)){
                    $this->share_user =  $this->session->userdata('share_user');
                }
            }

            $curr_url = 'http://'.$_SERVER['HTTP_HOST'].str_replace('/index.php','',$_SERVER['PHP_SELF']);
            // echo str_replace('/index.php','',$_SERVER['PHP_SELF']);
            if(empty($_SERVER['QUERY_STRING'])==false){
                $curr_url = $curr_url.'?'.$_SERVER['QUERY_STRING'];
            }

            if(strstr($_SERVER['PHP_SELF'],"logreg")==false&&strstr($_SERVER['PHP_SELF'],"login_wx")==false&&strstr($_SERVER['PHP_SELF'],"png")==false&&strstr($_SERVER['PHP_SELF'],"jpg")==false&&strstr($_SERVER['PHP_SELF'],"reg_msg")==false){
                $this->session->set_userdata('curr_url', $curr_url);
            }
            

            $this->load->library('session');
            $this->user_data = $this->session->userdata('user_data');
            $this->load->model('User_model');
            if(empty($this->user_data)){
                $this->logged_in = 0;
            }else{
                $user = $this->User_model->get_user_by_id($this->user_data['id']);
                if(empty($user))
                {
                  $this->session->sess_destroy();
                    unset(
                        $_SESSION['id'],
                        $_SESSION['head_image'],
                        $_SESSION['user_name'],
                        $_SESSION['mobile'],
                        $_SESSION['logged_in']
                    );
                    $this->logged_in = 0;
                    exit;
                }
                $this->logged_in = 1;

                $this->smarty->assign("user_data", $this->user_data);
            }
            $this->smarty->assign("logged_in", $this->logged_in);
            /*global $app_name;
            if($app_name != ''){*/
                $school_info = $this->User_model->get_school();
                $this->school_id = $school_info['id'];
                if(empty($this->school_id)){
                    show_error('您访问的站点不存在');exit;
                }
                $start_time = strtotime($school_info['start_date']);
                $end_time = strtotime($school_info['end_date']);
                $today_time = strtotime(date("Y-m-d"));
                if($start_time > $today_time || $end_time < $today_time){
                    show_error(" 该网校已经停止运营");exit;
                }
                $this->smarty->assign("school_info", $school_info);
                /*$menu_list = $this->school_menus();
                $menu_list_data = array();
                if(count($menu_list) > 0){
                    foreach ($menu_list as $key => $val) {
                        if($val['pid'] == 0){
                            $menu_list_data[$val['id']]['info'] = $val;
                        }else{
                            $menu_list_data[$val['pid']]['sub'][] = $val;
                        }
                    }
                }
                
                $page_list = $this->school_page();
                $this->smarty->assign("menu_list", $menu_list_data);
                $this->smarty->assign("page_list", $page_list['result']);*/
            /*}else{
                show_error('Error:domain prefix is null');
            }*/
        }

        function school_menus(){
            $this->load->model("School_menus");
            $menu_list = $this->School_menus->get_school_menus($this->school_id);
            return $menu_list;
        }

        function school_page(){
            $this->load->model("School_pages");
            $conditions['school_id'] = $this->school_id;
            $conditions['show_menu'] = 1;
            $page_list = $this->School_pages->get_list(0, 0, $conditions);
            return $page_list;
        }
	}

  class User_Controller extends MY_Controller{

    public $user_data = array();
    public $logged_in = 0;
   
    function __construct() {
        parent::__construct();

        $curr_url = 'http://'.$_SERVER['HTTP_HOST'].str_replace('/index.php','',$_SERVER['PHP_SELF']);
        // echo str_replace('/index.php','',$_SERVER['PHP_SELF']);
        if(empty($_SERVER['QUERY_STRING'])==false){
            $curr_url = $curr_url.'?'.$_SERVER['QUERY_STRING'];
        }
        
        if(strstr($_SERVER['PHP_SELF'],"logreg")==false)
            $this->session->set_userdata('curr_url', $curr_url);

        $this->load->library('session');
        $this->user_data = $this->session->userdata('user_data');

        if(empty($this->user_data)){
//            print_r("null");
            // redirect('/login');exit;
            //用户存在
            header("Location:"."http://{$_SERVER['HTTP_HOST']}/logreg");
            exit;
        }else{
            $this->load->model('User_model');
            $user = $this->User_model->get_user_by_id($this->user_data['id']);
            if(empty($user))
            {
                unset(
                    $_SESSION['id'],
                    $_SESSION['head_image'],
                    $_SESSION['user_name'],
                    $_SESSION['mobile'],
                    $_SESSION['logged_in']
                );
                header("Location:"."http://".$_SERVER['HTTP_HOST']."/logreg");
                exit;
            }
            $this->logged_in = 1;
            $this->smarty->assign("logged_in", $this->logged_in);
            $this->smarty->assign("user_data", $this->user_data);
        }


    }
	}
